#!/bin/bash
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
#
# git-version-bump - update versions and dates
#
# Copyright (C) 2025 Karel Zak <kzak@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

function show_help() {
	cat <<EOF
$(basename "$0") - update util-linux versions and release dates

Usage: $(basename "$0") [OPTIONS] <new-version>

This script updates NEWS file and release dates in configure.ac and meson.build
for a new util-linux release. It must be run from a stable/vX.Y branch.

Arguments:
  <new-version>    Version in format X.Y, X.Y.Z, X.Y-rcN, or X.Y.Z-rcN

Options:
  -h, --help       Show this help message

EOF
}

function validate_version() {
	local version="$1"

	# Valid formats: X.Y, X.Y.Z, X.Y-rcN, X.Y.Z-rcN
	if [[ ! "$version" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?(-rc[0-9]+)?$ ]]; then
		echo "Error: Invalid version format '$version'" >&2
		echo "Valid formats: X.Y, X.Y.Z, X.Y-rcN, X.Y.Z-rcN" >&2
		return 1
	fi
	return 0
}

function validate_branch() {
	local current_branch
	current_branch=$(git branch --show-current)

	if [[ ! "$current_branch" =~ ^stable/v[0-9]+\.[0-9]+$ ]]; then
		echo "Error: This script must be run from a stable/vX.Y branch" >&2
		echo "Current branch: $current_branch" >&2
		return 1
	fi
	return 0
}

# Parse arguments
case "$1" in
	-h|--help)
		show_help
		exit 0
		;;
	-*)
		echo "Error: Unknown option '$1'" >&2
		echo "Use --help for usage information." >&2
		exit 1
		;;
	"")
		echo "Error: Missing version argument" >&2
		echo "Usage: $(basename "$0") <new-version>" >&2
		echo "Use --help for more information." >&2
		exit 1
		;;
	*)
		VERSION="$1"
		;;
esac

# Verify we're in a Git repository
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
	echo "Error: Not inside a Git repository." >&2
	exit 1
}

# Validate version format
validate_version "$VERSION" || exit 1

# Validate branch
validate_branch || exit 1

function bump_news_version {
	local version="$1"
	local date=$(date +"%b %d %Y")

	sed -i "1s/.*/util-linux ${version}: ${date} (the latest and greatest!)/" NEWS
}

function bump_libblkid_date() {
   local current_date=$(date +"%d-%b-%Y")
 
   sed -i "s/LIBBLKID_DATE=\"[0-9]\{2\}-[A-Z][a-z]\{2\}-[0-9]\{4\}\"/LIBBLKID_DATE=\"$current_date\"/g" ./configure.ac
   sed -i "s/libblkid_date = '[0-9]\{2\}-[A-Za-z]\{3\}-[0-9]\{4\}'/libblkid_date = '$current_date'/g" meson.build
}

bump_news_version "$VERSION"
bump_libblkid_date

git commit -s -m "build-sys: update release dates" NEWS meson.build configure.ac 
